package com.minjiang.blog.controller.admin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.minjiang.blog.mode.Type;
import com.minjiang.blog.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.validation.Valid;
import java.util.List;

@Controller
@RequestMapping("/admin")
public class TypeController {

    @Autowired
    private TypeService typeService;
    /**
     *分页查询分类列表
     * @param model
     * @param pageNum
     * @return
     */
    @RequestMapping(value = "/types",method = RequestMethod.GET)
    public String types(Model model, @RequestParam(defaultValue = "1", value = "pageNum") Integer pageNum){
        String orderBy = "id desc";
        List<Type> allType = typeService.findAllType();
        //初始化设置
        PageHelper.startPage(pageNum,10,orderBy);
        //放入数据
        PageInfo<Type> pageInfo = new PageInfo<>(allType);
        //显示数据
        model.addAttribute("pageInfo",pageInfo);

        System.out.println(allType);
        return "admin/types";
    }
    //----------------------------------------------------------------------------------------

    /**
     * 返回新增分类页面
     * @return
     */
    @RequestMapping(value = "/types/input",method = RequestMethod.GET)
    public String typeInput(Model model){
        model.addAttribute("type",new Type());
        return "admin/type-add";
    }

    /**
     * 新增分类
     * @param type
     * @param redirectAttributes
     * @return
     * @Valid 用于实体数据的检验，错误自动获取message
     */
    @RequestMapping(value = "/types/add",method = RequestMethod.POST)
    public String typeAdd(@Valid Type type, RedirectAttributes redirectAttributes){
        Type typeByName = typeService.getTypeByName(type.getName());
        if (typeByName != null){
            redirectAttributes.addFlashAttribute("message","不能添加重复的分类");
            return "redirect:/admin/types/input";
        }
        int saveType = typeService.saveType(type);
        if (saveType > 0 ){
            redirectAttributes.addFlashAttribute("message","添加成功");
        }else {
            redirectAttributes.addFlashAttribute("message","添加失败");
        }
        return "redirect:/admin/types";
    }

    //----------------------------------------------
    /**
     *  跳转修改分类页面
     * @param id
     * @param model
     * @return
     * @Valid 用于实体数据的检验，错误自动获取message
     */
    @RequestMapping(value = "/types/update/{id}",method = RequestMethod.GET)
    public String typeRedirects(@PathVariable Long id, Model model){
        //将id搜索到的值赋予mode，用于页面的呈现
        model.addAttribute("mode",typeService.findType(id));
        return "admin/type-input";
    }

    /**
     * 编辑修改页面
     * @param
     * @param type
     * @param redirectAttributes
     * @return
     */
    @RequestMapping(value ="/types/edit/{id}",method = RequestMethod.POST)
    public String typeEdit( @Valid  Type type,RedirectAttributes redirectAttributes){
        Type typeByName = typeService.getTypeByName(type.getName());
        if (typeByName != null){
            redirectAttributes.addFlashAttribute("message","不能添加重复的分类");
            return "redirect:admin/types/input";
        }
        int saveType = typeService.updateType(type);
        if (saveType >0){
            redirectAttributes.addFlashAttribute("message","编辑成功");
        }else {
            redirectAttributes.addFlashAttribute("message","编辑失败");
        }
        return "redirect:/admin/types";
    }

    /**
     * 删除分类
     * @param id
     * @param redirectAttributes
     * @return
     */
    @RequestMapping(value = "/types/delete/{id}", method = RequestMethod.GET)
    public String typeDelete(@PathVariable("id") Long id, RedirectAttributes redirectAttributes){
        typeService.deleteType(id);
        redirectAttributes.addFlashAttribute("message","删除成功");
        return "redirect:/admin/types";
    }

}
